package com.cfec.educationplatform.Controller;

import com.cfec.educationplatform.domain.Chat;
import com.cfec.educationplatform.service.impl.TbAichatHistoryServiceImpl;
import com.cfec.educationplatform.util.AliOSSUtils;
import com.cfec.educationplatform.util.OkHttpExampleUtil;
import com.cfec.educationplatform.util.ResultData;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/ai")
@ApiOperation("/控制层:ai")
@Slf4j
public class AIContorller {

    @Resource(name = "okHttpExampleUtil")
    private OkHttpExampleUtil okHttpExampleUtil;

    @Resource(name = "aliOSSUtils")
    private AliOSSUtils aliOSSUtils;

    @Resource(name = "tbAichatHistoryServiceImpl")
    private TbAichatHistoryServiceImpl tbAichatHistoryService;

    @PostMapping("/getAi")
    @ApiOperation("/测试")
    public ResultData<Object> getAi(@RequestBody Map<String, String> requestBody) throws JSONException {
        System.out.println("url:"+requestBody);
        return okHttpExampleUtil.GetHomeworkDetail(requestBody.get("url"));
    }

    @GetMapping("/getHistory")
    @ApiOperation("/获取ai辅导的历史记录")
    public ResultData<Object> getHistory(@RequestParam("userId") Long userId) {
        return tbAichatHistoryService.GetHistory(userId);
    }

    @PostMapping("/getAIPractice")
    @ApiOperation("/获取ai练习的题目")
    public ResultData<Object> getAIPractice(@RequestBody Map<String, String> requestBody) throws JSONException {
        System.out.println("url:"+requestBody);
        return okHttpExampleUtil.GetAIPractice(requestBody);
    }

    @PostMapping("/getAiErrCorrecting")
    @ApiOperation("/获取作业批改结果")
    public ResultData<Object> getAiErrCorrecting(@RequestParam("image") MultipartFile image) throws IOException, JSONException {

        if (image == null) {
            return new ResultData<>(200, "img为null, 请求失败", null);
        }

        String URL = aliOSSUtils.upload(image);
        log.info("图片地址为: {}",URL);
        return okHttpExampleUtil.GetHomeworkDetail(URL);
    }

    @PostMapping("/ChatAi")
    @ApiOperation("/上传用户信息并获取ai的聊天信息")
    public ResultData<Object> ChatAi(@RequestBody Chat chat) throws JSONException {
        System.out.println("chat:"+ chat);
        return okHttpExampleUtil.Chat(chat);
    }


}
